package com.ymatou.iapi.sellerproduct.testcase;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.QuerySellerProductListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ActivityCallService;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductsInActivity;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_QuerySellerProductList{
	private static String sellerName = EnvSetup.getData("productsellername");
	private static String sellerId = EnvSetup.getData("productsellerid");
	private static String activityStartId = EnvSetup.getData("productActivityStartId");
	
	private static String dateformat = "MM-dd HH:mm:ss";
	private static Map<String, Object> productOptionMap = new HashMap<String,Object>();
	
	//private static String activityNotStartId = EnvSetup.getData("productActivityNotStartId");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品管理列表查询");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new QuerySellerProductListCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	//仅现货_未开始_有库存
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInStock_NotStart_HasStock() {
		Logger.start(true, "仅现货_未开始_有库存，期待结果  状态：[下架]， 属性：[空]");
		try{
			//1.添加商品 -定时上架
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货下架_" + YMTDateUtil.getSysTime(dateformat));
			JSONObject productReqObject = SellerProductCallService.addNewProductOnTime(Integer.parseInt(sellerId), sellerName, productOptionMap);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			YMTDateUtil.waitTime(1);
			
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//2. 查询该商品
			//check 商品状态为下架 status = 2
			List<Integer> tgtStatusList = new ArrayList<Integer>();
			tgtStatusList.add(2);
			
			//check 商品销售属性为活动 saleKind =空
			List<Integer> tgtSaleKindList = new ArrayList<Integer>();		
			
			checkProductStatus(productID,tgtStatusList,tgtSaleKindList);
						

		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	//仅现货_已结束_有库存
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInStock_End_HasStock() {
		Logger.start(true, "仅现货_已结束_有库存，期待结果  状态：[下架]， 属性：[空]");
		try{
			//1.添加商品 -定时上架
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货下架_" + YMTDateUtil.getSysTime(dateformat));
			JSONObject productReqObject = SellerProductCallService.addNewProductNotOnSale(Integer.parseInt(sellerId), sellerName, productOptionMap);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			YMTDateUtil.waitTime(1);
			
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//2. 查询该商品
			//check 商品状态为下架 status = 2
			List<Integer> tgtStatusList = new ArrayList<Integer>();
			tgtStatusList.add(2);
			
			//check 商品销售属性为活动 saleKind =空
			List<Integer> tgtSaleKindList = new ArrayList<Integer>();		
			
			checkProductStatus(productID,tgtStatusList,tgtSaleKindList);
						

		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	//仅现货_进行中_有库存
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInStock_Start_HasStock(){
		Logger.start(true, "仅现货_进行中_有库存，期待结果  状态：[上架]， 属性：[现货]");
		try{
			//1.添加商品 -定时上架
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货上架_" + YMTDateUtil.getSysTime(dateformat));
			JSONObject productReqObject = SellerProductCallService.addNewProductOnSale(Integer.parseInt(sellerId), sellerName, productOptionMap);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			YMTDateUtil.waitTime(1);
		
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//2. 查询该商品
			//check 商品状态为上架 status = 1
			List<Integer> tgtStatusList = new ArrayList<Integer>();
			tgtStatusList.add(1);
			
			//check 商品销售属性为现货  saleKind =1
			List<Integer> tgtSaleKindList = new ArrayList<Integer>();		
			tgtSaleKindList.add(1);
			
			checkProductStatus(productID,tgtStatusList,tgtSaleKindList);
						

		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
		
	//仅直播_开始直播_销售中
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInLive_Start_OnSale() {
		
		Logger.start(true, "仅直播_开始直播_销售中，期待结果  状态：[上架]， 属性：[直播]");
		try{
			//1.添加商品 -不上架
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货下架_直播进行中销售中_" + YMTDateUtil.getSysTime(dateformat));
			JSONObject productReqObject = SellerProductCallService.addNewProductNotOnSale(Integer.parseInt(sellerId), sellerName, productOptionMap);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//2. 创建 正在进行中的直播
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, Integer.valueOf(sellerId));
			Logger.verifyEquals(liveResponseBean.getInt("Code"), 200, "响应报文-返回码");
			
			//获取LiveId
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 商品加入直播
			SellerProductCallService.addProductInLive(Integer.valueOf(sellerId), liveId, productID);
		
			YMTDateUtil.waitTime(1);
			
			//4. 查询该商品
			//check 商品状态为上架 status = 1
			List<Integer> tgtStatusList = new ArrayList<Integer>();
			tgtStatusList.add(1);
			
			//check 商品销售属性为直播 saleKind =2
			List<Integer> tgtSaleKindList = new ArrayList<Integer>();
			tgtSaleKindList.add(2);
			
			checkProductStatus(productID,tgtStatusList,tgtSaleKindList);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}


	//仅直播_开始直播_暂停销售
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInLive_Start_OnSuspend(){
		Logger.start(true, "仅直播_开始直播_暂停销售，期待结果  状态：[下架]， 属性：[直播]");
		try{
			//1.添加商品 -不上架
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货下架_直播进行中暂停销售_" + YMTDateUtil.getSysTime(dateformat));
			JSONObject productReqObject = SellerProductCallService.addNewProductNotOnSale(Integer.parseInt(sellerId), sellerName, productOptionMap);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//2. 创建 正在进行中的直播
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, Integer.valueOf(sellerId));
			Logger.verifyEquals(liveResponseBean.getInt("Code"), 200, "响应报文-返回码");
			
			//获取LiveId
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 商品加入直播
			SellerProductCallService.addProductInLive(Integer.valueOf(sellerId), liveId, productID);
		
			//4. 暂停销售
			SellerProductCallService.suspendSaleProductInLive(Integer.parseInt(sellerId), liveId, productID);
			
			YMTDateUtil.waitTime(1);
			
			JSONObject requestObject = SellerProductCallService.queryProduct(sellerId, productID);
			Logger.verifyEquals(requestObject.getInt("Code"), 200, "响应报文-返回码");
			//check 该商品的状态和当前销售属性
			JSONObject queryProductObject = requestObject.getJSONObject("Data").getJSONArray("ProductList").getJSONObject(0);
			
			//check 商品状态为下架 status = 2
			JSONArray statusAry = queryProductObject.getJSONArray("StatusList");
			Logger.verifyEquals(1,statusAry.length(), "商品状态");
			Logger.verifyEquals(2,statusAry.get(0), "商品状态");
			
			//check 商品销售属性为活动直播 saleKind =2
			JSONArray saleKindAry = queryProductObject.getJSONArray("SaleKindList");
			Logger.verifyEquals(1,saleKindAry.length(), "商品销售属性");
			Logger.verifyEquals(2,saleKindAry.get(0), "商品销售属性");
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	//仅活动_进行中_有活动库存
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInActivity_Start_HasStock() {
		Logger.start(true, "仅活动_进行中_有活动库存，期待结果  状态：[上架]， 属性：[活动]");
		try{
			//添加商品 参加活动 并审核通过
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货下架_活动进行中有库存_" + YMTDateUtil.getSysTime(dateformat));
			String productID = addNewProductAndApproveActivity(activityStartId,productOptionMap);
			
			YMTDateUtil.waitTime(1);
			
			//check 商品状态为上架 status = 1
			List<Integer> tgtStatusList = new ArrayList<Integer>();
			tgtStatusList.add(1);
			
			//check 商品销售属性为活动 saleKind =3
			List<Integer> tgtSaleKindList = new ArrayList<Integer>();
			tgtSaleKindList.add(3);
			
			checkProductStatus(productID,tgtStatusList,tgtSaleKindList);
			
		}catch(Exception e){
			Logger.fail(e);
		}
			
	}
	
	
	//仅活动_进行中_无活动库存
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductInActivity_Start_NoStock() {
		Logger.start(true, "仅活动_进行中_无活动库存，期待结果  状态：[下架]， 属性：[空]");
		try{
			//发布商品 参加活动
			productOptionMap.clear();
			productOptionMap.put("Product->ProductName", "自动化_现货下架_活动进行中无库存_" + YMTDateUtil.getSysTime(dateformat));
			String productID = addNewProductAndApproveActivity(activityStartId,productOptionMap);
			
			YMTDateUtil.waitTime(1);
			
			//修改活动库存为0
			YmtProductActivityStockWapper ymtProductActivityStockWapper  = new YmtProductActivityStockWapper();
			ymtProductActivityStockWapper.updateStockByActivityIdAndProductId(productID,Integer.parseInt(activityStartId),0);
			
			
			
			//check 商品状态为下架 status = 2
			List<Integer> tgtStatusList = new ArrayList<Integer>();
			tgtStatusList.add(2);
			
			//check 商品销售属性为空 saleKind 
			List<Integer> tgtSaleKindList = new ArrayList<Integer>();
			
			checkProductStatus(productID,tgtStatusList,tgtSaleKindList);

			
		}catch(Exception e){
			Logger.fail(e);
		}	
	}
	
	
	
	
	/////////////////////////////////////////////////////////////////////////
	/***
	 * 检查商品状态和销售属性
	 * @param productId
	 * @param tgtStatusList
	 * @param tgtSaleKindList
	 * @return
	 * @throws Exception
	 */
	private JSONObject checkProductStatus(String productId, List<Integer> tgtStatusList,List<Integer> tgtSaleKindList) throws Exception {
		JSONObject requestObject = SellerProductCallService.queryProduct(sellerId, productId);
		Logger.verifyEquals(requestObject.getInt("Code"), 200, "响应报文-返回码");
		
		//check 该商品的状态和当前销售属性
		JSONObject queryProductObject = requestObject.getJSONObject("Data").getJSONArray("ProductList").getJSONObject(0);
		
		//check 商品状态
		JSONArray actStatusAry = queryProductObject.getJSONArray("StatusList");
		Logger.verifyEquals(tgtStatusList.size(),actStatusAry.length(), "响应报文-商品状态");
		for (int i = 0; i<actStatusAry.length();i++){Logger.verifyEquals(true,tgtStatusList.contains(actStatusAry.getInt(i)), "响应报文-商品状态");}
		
		//check 商品销售属性
		JSONArray actSaleKindAry = queryProductObject.getJSONArray("SaleKindList");
		Logger.verifyEquals(tgtSaleKindList.size(),actSaleKindAry.length(), "响应报文-商品销售属性");
		for (int i = 0;i<actSaleKindAry.length();i++){Logger.verifyEquals(true,tgtSaleKindList.contains(actSaleKindAry.getInt(i)), "响应报文-商品销售属性");}

		
		return requestObject;
	}
	
	
	
	/***
	 * 发布商品并申请参加活动
	 * @param activityId
	 * @return
	 */
	private String addNewProductAndApproveActivity(String activityId,Map<String, Object> productUpdateMap){
		try{
			//1.添加商品 -不上架
			JSONObject productReqObject = SellerProductCallService.addNewProductNotOnSale(Integer.parseInt(sellerId), sellerName, productUpdateMap);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			
			//获取Catalogs id - SQL Ymt_Catalogs integratedproduct库
			String catalogsID = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(0).getsCatalogId();

		
			//todo 活动id
			//2.参加活动 
			JSONObject applyActivityRequest = ActivityCallService.applyProductInActivity(Integer.parseInt(sellerId), productID, catalogsID, activityId);
			Logger.verifyEquals(applyActivityRequest.getInt("Code"), 200, "响应报文-返回码");
			
			//查找productInActivityId
			YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();	
			YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper.selectByProductId(productID);
			int productInActivityId = productsInActivity.getIproductinactivityid();
			//3. 开始活动二审
			//3.1 审核通过
			JSONObject approveProductRequest = AdminCallService.approveProductInActivity(String.valueOf(productInActivityId));		
			
			//验证审核通过
			String msgStr = approveProductRequest.getString("Success").equals("true")?"活动审核通过":"活动审核不通过";
			Logger.comment("++++++"+msgStr+"++++++");
			
			return productID;
		}catch(Exception e){
			Logger.fail(e);
		}
		return null;
		
		
	}


	//test
		public static void main(String[] args) {
			Integer a=1;
			System.out.print(a==1);
		}
		


}